<%@ page import="com.deepai.dprsmgr.utils.ErrCode" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@include file="../base_path.jsp" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="description" content="">
    <%--<link rel="stylesheet" type="text/css" href="<%=basePath%>/lib/bootstrap/3.3.7/css/bootstrap.min.css"/>--%>
    <%--<link type="text/css" rel="stylesheet" href="<%=basePath%>/lib/bootstrap-select/css/bootstrap-select.min.css"/>--%>
    <%--<link rel="stylesheet" type="text/css" media="screen" href="<%=basePath%>/lib/lobibox/css/lobibox.min.css"/>--%>
    <%--<link rel="stylesheet" type="text/css" media="screen" href="<%=basePath%>/css/mycss.css"/>--%>
    <title>密码修改</title>
	<style type="text/css">
		body {
			background: #FFF;
		}

		.control-label {
			text-align: right;
		}
	</style>
</head>
<body>
<div class="modal-header">
	<button id="btn_close" type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	<h4 class="modal-title">创建账户</h4>
</div>
<div class="modal-body">

    <div class="container-fluid container-fluid-w" style="background: #FFF;width: 90%;margin-top: 20px;">
        <form id="form_create_account" class="form-horizontal" style="padding: 20px;">
            <div class="form-group">
                <label for="txt_account_name" class="col-xs-3 control-label">
                    <label class="text-danger" style="margin-right: 5px">*</label>用户名</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control input-sm" id="txt_account_name" name="txt_account_name"
                           placeholder="只支持字母、数字、下划线" maxlength="60">
                    <span class="help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="txt_account_passwd_for_create" class="col-xs-3 control-label">
                    <label class="text-danger" style="margin-right: 5px">*</label>密码</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control input-sm" id="txt_account_passwd_for_create" name="txt_account_passwd_for_create"
                           placeholder="只支持字母、数字、下划线" maxlength="16">
                    <span class="help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-xs-3 control-label">
                    <label class="text-danger" style="margin-right: 5px">*</label>角色权限</label>
                <div class="col-xs-8">
                    <c:forEach var="item" items="${roleListForCreate}">
                        <label class="radio-inline">
                            <input type="radio" name="inlineRadioOptions" value="${item.id}"> ${item.roleName}
                        </label>
                    </c:forEach>
                    <span class="help-block"></span>
                </div>
            </div>
            <div class="form-group">
                <label for="txt_org_create" class="col-xs-3 control-label">
                    <label class="text-danger" style="margin-right: 5px">*</label>所属组织</label>
                <div class="col-xs-8">

                   <shiro:hasAnyRoles name="role_super_admin,role_admin">
						<select id="txt_org_create" class="selectpicker form-control input-sm" name="txt_org_create"
								data-live-search="true" title="请选择"
								data-live-search-placeholder="Search" data-actions-box="true">
							<c:forEach var="item" items="${orgListForCreate}">
								<option value="${item.id}">${item.orgName}</option>
							</c:forEach>
						</select>
					</shiro:hasAnyRoles>
					<shiro:hasAnyRoles name="dep_leader,quality_specialist,analysis_specialist,call_specialist">
							<select id="txt_org_create" class="selectpicker form-control input-sm" name="txt_org_create"
									 disabled
									 data-actions-box="true">
							<c:forEach var="item" items="${orgListForCreate}">
								<option value="${item.id}" selected>${item.orgName}</option>
							</c:forEach>
							</select>
					</shiro:hasAnyRoles>

                    <span class="help-block"></span>
                </div>
            </div>


            <div class="form-group text-c">
                    <button type="submit" class="btn btn-primary btn-sm btn-s">提交</button>
            </div>
        </form>
    </div>

</div>
<%--<script type="text/javascript" src="<%=basePath%>/lib/jquery/1.11.0/jquery-1.11.0.min.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/bootstrap/3.3.7/js/bootstrap.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/bootstrap-select/js/bootstrap-select.min.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/bootstrap-select/js/i18n/defaults-zh_CN.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/jquery.validation/1.14.0/jquery.validate_1.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/jquery.validation/1.14.0/validate-methods.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/lobibox/js/lobibox.min.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/lobibox/js/messageboxes.min.js"></script>--%>
<%--<script type="text/javascript" src="<%=basePath%>/lib/lobibox/js/notifications.min.js"></script>--%>
<script type="text/javascript" src="<%=basePath%>/lib/JQuery.md5.js"></script>
<%--<script type="text/javascript" src="<%=basePath%>/js/common.js"></script>--%>


<script type="text/javascript">

    $(function () {
		$("#txt_org_create").selectpicker();
        // 对象及变量声明
        $("#form_create_account").validate({
            errorPlacement: function (error, element) {
                // Append error within linked label
                //console.log(element)
                $(element)
                    .closest("div.form-group")
                    .find("span.help-block")
                    .append(error);
            },
            errorElement: "p",
            rules: {
                txt_account_name: {
                    required: true,
                    isMaxlen: 60,
                    isName: true,
                    remote:{
                        type:"POST",
                        url:"<%=basePath%>/account/getUserByUname.do", //请求地址
                        data:{
                            uname:function(){ return $("#txt_account_name").val(); }
                        } ,
                        dataFilter:function(data){
                            if(JSON.parse(data).errorCode == "2001"){
                                return false;
                            }else{
                                return true;
                            }
                        }
                    }
                },
                txt_account_passwd_for_create: {
                    required: true,
                    isPassword2: true,
                    minlength: 6,
                },
                inlineRadioOptions: {
                    required: true
                },
                txt_org_create: {
                    required: true
                }
            },
            messages: {
                txt_account_name: {
                    required: "用户名称不能为空，请输入用户名",
					isName: "字符或格式有误，只支持字母、数字、下划线",
                    remote:"用户名已存在，请重新输入"
                },
                txt_account_passwd_for_create: {
					required: "密码称不能为空，请输入密码",
					isPassword2: "字符或格式有误，请输入6-16个字符，只能包含字母、数字以及标点符号，至少包含2种字符类型",
					minlength: "至少输入6位字符",
                },
                inlineRadioOptions: {
                    required: "角色不能为空，请选择"
                },
                txt_org_create: {
                    required: "所属组织不能为空，请选择"
                }
            }
        });
        $("input[type='radio'][name='inlineRadioOptions']").change(function () {
            //2是域管理员 3是管理员
            if(this.value == "2"){
                $("#txt_org_create").closest(".form-group").hide();
            }else{
                $("#txt_org_create").closest(".form-group").show();
                if(this.value == "3"){
                    $("#txt_org_create").blur();
                }else{
                }
            }
        })

//        $('#txt_org_create').on('changed.bs.select',txt_myorg_cbs);
        <shiro:hasAnyRoles name="operator_leader,role_admin,quality_specialist,analysis_specialist,call_specialist">
            firstOrgFalg=true;
        </shiro:hasAnyRoles>


        $('.selectpicker').on('hide.bs.select', function (e) {
            $(this).focusout();
            if ($(this).val() == null) {
                $(this).addClass("has-error").siblings(".btn").addClass("redBorder");
            } else {
                $(this).removeClass("has-error").siblings(".btn").removeClass("redBorder");
            }
        });

        // 表单提交
        $("#form_create_account").submit(function (event) {
            console.log("----------------" + $("#txt_account_passwd_for_create").val());
            if ($("#form_create_account").valid()) {
                // 这里用的是this，如果是Form的话需要Form[0]
                var formData = new FormData(this);
                formData.append("username", $.trim($("#txt_account_name").val()));
                console.log("----------------" + $("#txt_account_passwd_for_create").val());
                console.log("===============" + $.trim($("#txt_account_passwd_for_create").val()));
                formData.append("password", hex_md5($.trim($("#txt_account_passwd_for_create").val())));
                var roleId=$("input[name='inlineRadioOptions']:checked").val();
                formData.append("roleId", roleId);
                if(roleId !="2"){
                    formData.append("orgId", $("#txt_org_create").val());
                }
                // 阻止当前提交事件，自行实现，否则会跳转
                event.preventDefault();
                $.ajax({
                    url: '<%=basePath%>/account/userCreate.do',
                    type: 'POST',
                    data: formData,
                    async: false,
                    // 这两个参数需要被定义，否则报错
                    contentType: false,
                    processData: false,
                    traditional: true,
                    success: function (data) {
                        console.log(data);
                        if (data["errorCode"] == "0") {
                            parent.myNotify("success", "", "账户创建成功！");
                            resetForm();
							var index = parent.layer.getFrameIndex(window.name);
							parent.refreshJQGrid()
							parent.layer.close(index);

                        } else {
                            myNotify("error", "", data["msg"]);
                        }
                    },
                    error: function () {
                            myNotify("error", "", "账户创建过程中发生错误，请联系管理员！");
                    }
                });
            } else {
                    myNotify("error", "", "请更正错误后在进行提交");
            }
            return false;
        });
    });

    /******************************方法声明**********************************/
    // 重置表单
    function resetForm() {
		var hasPower=0;
		<shiro:hasAnyRoles name="role_super_admin">
		    hasPower=1;
		</shiro:hasAnyRoles>
		<shiro:hasAnyRoles name="role_admin">
		    hasPower=2;
		</shiro:hasAnyRoles>
		<shiro:hasAnyRoles name="operator_leader">
		    hasPower=3;
		</shiro:hasAnyRoles>
		<shiro:hasAnyRoles name="quality_specialist">
		     hasPower=4;
		</shiro:hasAnyRoles>
		<shiro:hasAnyRoles name="analysis_specialist">
		    hasPower=5;
		</shiro:hasAnyRoles>
		<shiro:hasAnyRoles name="call_specialist">
		hasPower=6;
		</shiro:hasAnyRoles>
        $("#form_create_account")[0].reset();
        if(hasPower <=2){
            $('#txt_org_create').selectpicker('val', '');
        }
    }
    var firstOrgFalg=false;
</script>

</body>
</html>